const util = require("../../../utils/util")
const domainName = getApp().globalData.domainName
const oss_domain = getApp().globalData.oss_domain
const parameter = getApp().globalData.parameter

// 加载某天的日常练习
const load_daily_records=(that,jg_id,date_set)=>{
  wx.showLoading({
    title: '加载中...',
  })
  wx.request({
    url: domainName + '/load_daily_records',
    data: {
      jg_id,
      date_set,
    },...parameter,
    success: res => {
      wx.hideLoading()
      that.setData({
        daily_exercises:res.data.daily_exercises,
        testPapers:res.data.testPapers,
        active_item:'daily_exercises',  // 默认显示日常练题
      })
    },
  })
}


// 页面初始化
const page_init=(that,jg_id)=>{
  wx.showLoading()
  wx.request({
    url: domainName + '/daily_records_init',
    data: {
      jg_id:jg_id
    },...parameter,
    success: res => {
      wx.hideLoading()
      let date_list = res.data.date_list
      if(date_list.length>0){
        that.setData({
          date_list:date_list, // 加载最近30日
          daily_exercises:res.data.daily_exercises, //加载最近一天的做题情况
          testPapers:res.data.testPapers, // 加载最近一天的试卷情况
          selected_date:date_list[date_list.length-1],  // 最近一天为默认选定日
          active_item:'daily_exercises',  // 默认显示日常练题
        })
      }
      that.setData({
        loading:false
      })
    },
  })
}


// 加载某试卷的成绩单列表
const load_transcripts_by_paper=(that,test_paper_id)=>{
  wx.showLoading({
    title: '加载中...',
  })
  wx.request({
    url: domainName + '/load_transcripts_by_paper',
    data: {
      test_paper_id,
    },...parameter,
    success: res => {
      wx.hideLoading()
      that.setData({
        transcripts:res.data.transcripts,
      })
    },
  })
}

// pages/class_theory_test_rank/class_theory_test_rank.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading:true,
    icon_url:oss_domain + 'static/picture/app_icon/',
    windowWidth : wx.getSystemInfoSync().windowWidth,
    today: util.formatDate(new Date()),
    thisYear:new Date().getFullYear(),
    label_id:'',
    th_stu_list:[],
    date_list:[],
    daily_exercises:[],
    testPapers:[],
  },


  // 选择考试日期
  sel_date(e){
    getApp().get_vibration()
    let index = e.currentTarget.id.slice(2,)
    var selected_date = this.data.date_list[index]
    this.setData({
      selected_date:selected_date,
    })
    // 加载当天的日常练题情况和试卷列表
    load_daily_records(this, this.jg_id, selected_date)

  },


  // 选择日常练题
  sel_daily_exercises(){
    getApp().get_vibration()
    this.setData({
      active_item:'daily_exercises',
    })
  },

  // 选择试卷
  sel_test_paper(e){
    getApp().get_vibration()

    let index = e.currentTarget.id
    this.setData({
      active_item:'test_paper',
      test_paper_act:index,
    })
    let test_paper_id = this.data.testPapers[index].test_paper_id
    load_transcripts_by_paper(this,test_paper_id)
  },


  // 显示今日易错100题
  show_wrongs(){
    getApp().get_vibration()
    wx.navigateTo({
      url: '/pages/doTi/wrongs_ofJg/wrongs_ofJg?jg_id=' + this.jg_id + '&date_set=' + this.data.selected_date,
    })
  },
  

  // 查看该学员成绩单
  view_transcript_detail(e){
    getApp().get_vibration()
    let index = e.currentTarget.id
    let transcript_id = this.data.transcripts[index].transcript_id
    wx.navigateTo({
      url: '/pages/doTi/transcript_details/transcript_details?transcript_id=' + transcript_id,
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.jg_id = options.jg_id
    page_init(this, this.jg_id)
  },


onReady(){
  this.setData({
    windowHeight : wx.getSystemInfoSync().windowHeight,
  })
},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '【训练统计表】' + this.data.today,
      path: '/pages/students/daily_records/daily_records?jg_id=' + this.jg_id 
    }
  }
})